# -*- coding: utf-8 -*-

# Define your item pipelines here
#
# Don't forget to add your pipeline to the ITEM_PIPELINES setting
# See: http://doc.scrapy.org/en/latest/topics/item-pipeline.html

import pymysql
from scrapy.conf import settings
from scrapy import log


class MyspiderPipeline(object):

    def __init__(self):
        _conn = pymysql.connect(
            host = settings['MYSQL_HOST'],
            port = settings['MYSQL_PORT'],
            user = settings['MYSQL_USER'],
            passwd = settings['passwd'],
            db = settings['UM_DB'],
            charset = 'utf8'
        )
        self.cursor = _conn.cursor(cursor = pymysql.cursors.DictCursor)

    def process_item(self, item, spider):
        if spider.name == 'umeng':
            _sql_execute = "INSERT INTO umeng_xys.app_channels_copy(package, date, channel, num) VALUES ('%s','%s','%s', %d)" % (item['package'], item['date'], item['channel'], item['quantity'])
            print(_sql_execute)
            try:
                self.cursor.execute(_sql_execute)
            except Exception as e:
                log.msg(e)
            return item

    def close_spider(self,spider):
        self.cursor.close()